﻿using GN.Pay.TradeChannel.DefaultImpl;
using GN.Pay.Utils;
using GN.Pay.Utils.Reflection;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace GN.Pay.TradeChannel.WeiXin.Response
{
    [Serializable]
    class PayNotifyResponse : AResponse
    {
        public PayNotifyResponse(string xml)
            : base(xml)
        {
        }

        /// <summary>
        /// 应用Id
        /// </summary>
        public string AppId { get; set; }

        /// <summary>
        /// 商户号
        /// </summary>
        public string Mch_Id { get; set; }

        /// <summary>
        /// 设备信息
        /// </summary>
        public string Device_Info { get; set; }

        /// <summary>
        /// 用户在商户appid下的唯一标识
        /// </summary>
        public string OpenId { get; set; }

        /// <summary>
        /// 用户是否关注公众账号，Y-关注，N-未关注，仅在公众账号类型支付有效
        /// </summary>
        public string Is_Subscribe { get; set; }

        /// <summary>
        /// 交易类型 JSAPI、NATIVE、APP
        /// </summary>
        public string Trade_Type { get; set; }

        /// <summary>
        /// 付款银行
        /// </summary>
        public string Bank_Type { get; set; }

        /// <summary>
        /// 订单金额
        /// </summary>
        public long Total_Fee { get; set; }

        /// <summary>
        /// 应结订单金额,应结订单金额=订单金额-非充值代金券金额，应结订单金额 小于或等于 订单金额。 
        /// </summary>
        public long Settlement_Total_Fee { get; set; }

        /// <summary>
        /// 货币种类
        /// </summary>
        public string Fee_Type { get; set; }

        /// <summary>
        /// 现金支付金额
        /// </summary>
        public long Cash_Fee { get; set; }

        /// <summary>
        /// 现金支付货币类型
        /// </summary>
        public string Cash_Fee_Type { get; set; }

        /// <summary>
        /// 微信支付号
        /// </summary>
        public string Transaction_Id { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string Out_Trade_No { get; set; }

        /// <summary>
        /// 数据包，原样返回
        /// </summary>
        public string Attach { get; set; }

        /// <summary>
        /// 支付时间
        /// </summary>
        public string Time_End { get; set; }

        /// <summary>
        /// 获取付款时间
        /// </summary>
        /// <returns></returns>
        public DateTime? PayTime()
        {
            if (string.IsNullOrWhiteSpace(Time_End))
            {
                return null;
            }

            var format = new DateTimeFormatInfo();
            return DateTime.Now;
            //  return DateTime.Parse(Time_End, format);
        }        


        public ITradeOrderQueryResult ToPayResult(ITradeChannel channel)
        {
            if (string.IsNullOrWhiteSpace(this.Out_Trade_No))
            {
                channel.ThrowChannelValidationException("微信交易付款通知的商户订单号为null或空白。");
            }
            if (this.Total_Fee <= 0)
            {
                channel.ThrowChannelValidationException("微信交易付款通知总金额小于或等于零。");
            }
            var payTime = this.PayTime();
            if (payTime == null)
            {
                channel.ThrowChannelValidationException("微信交易付款通知付款时间为 null。");
            }
            return new TradeOrderQueryResult(channel)
            {
                OrderNo = Out_Trade_No,
                PayTime = payTime.Value,
                TradeNo = Transaction_Id,
                OrderMoney = Math.Round((decimal)this.Total_Fee / (decimal)100, 2),
                Subject = "",
                BankType = Bank_Type,
                Body = "",
                PayFee = 0m,
                RefundFee = 0m,
                RefundMoney = 0m,
                ExtraParam = Attach,
            };
        }
    }
}
